//
//  AryaCoreRoute.swift
//  Pods
//
//  Created by eme on 2017/3/24.
//
//

import UIKit
import UtilCore
import URLNavigator
import RxSwift
import NetWorkCore

///首页模块的路由
public struct AryaCoreRoute {
    
    static let disposeBag = DisposeBag()
    
    public static func initialize() {
        ///跳转到购物券
        Navigator.map("/coupons") { url, values in
            let gwqVc:BaseCouponsVc = BaseCouponsVc()
            gwqVc.hidesBottomBarWhenPushed = true
            Navigator.push(gwqVc)
            return true
        }
        ///新增地址
        Navigator.map("/addnewaddress") { url, values in
            let addNewAddressVc:AddNewAddress_Vc = AryaCore.storyboard.instantiateViewController()
            addNewAddressVc.addid = url.queryParameters["addid"]
            Navigator.push(addNewAddressVc)
            return true
        }
        ///地址管理列表
        Navigator.map("/magagenewaddress") { url, values in
            let manageAddressVc:ManageAddressList_Vc = AryaCore.storyboard.instantiateViewController()
            Navigator.push(manageAddressVc)
            return true
        }
        ///地址选择列表
        Navigator.map("/addresslist") { url, values in
            let addressListVc:AddressList_Vc = AryaCore.storyboard.instantiateViewController()
            Navigator.push(addressListVc)
            return true
        }
        /// 跳转到 铺货确定界面
        Navigator.map("/cofirmorder") { url, values in
            let cofirmVc:CofirmOrder_Vc = AryaCore.storyboard.instantiateViewController()
            
            guard let osCarts = url.queryParameters["osCarts"], osCarts.characters.count > 0 else{
                showMsg("添加订单数据错误")
                return false
            }
            guard let iscart = url.queryParameters["iscart"], iscart.characters.count > 0 else{
                showMsg("参数错误")
                return false
            }
            ///添加混批参数
            let ismix = url.queryParameters["ismix"] ?? "0"
            let item:(osCarts:NSString,iscart:String) = (osCarts as NSString,iscart)
            cofirmVc.cofirmParam = item
            cofirmVc.ismix = ismix
            Navigator.push(cofirmVc)
            return true
        }
        Navigator.map("/pay") { url, values in
            let PayVc:Pay_Vc = AryaCore.storyboard.instantiateViewController()
            let payid = url.queryParameters["payid"]
            let orderId = url.queryParameters["orderid"]
            let giveId = url.queryParameters["giveid"]
            let ordertype = url.queryParameters["ordertype"]
            if orderId == nil && payid == nil{
                showMsg("参数错误")
                return false
            }
            guard let ordersumamount = url.queryParameters["ordersumamount"], ordersumamount.characters.count > 0 else{
                showMsg("支付金额错误")
                return false
            }
            PayVc.giveid = giveId
            PayVc.order_type = ordertype
            PayVc.orderId = orderId
            PayVc.payid = payid
            PayVc.orderSumAmount = ordersumamount
            Navigator.push(PayVc)
            return true
        }
        ///跳转到支付成功界面
        Navigator.map("/paysuccesspage") { url, values in
            let paySuccessVc:PaySuccessPage_Vc = AryaCore.storyboard.instantiateViewController()
            /// 1：支付成功 2:支付失败 3:取消支付
            paySuccessVc.showType = url.queryParameters["showtype"]
            Navigator.push(paySuccessVc)
            return true
        }
        /// 弹出错误提示信息
        Navigator.map("/orderformbadge"){ url, values in
            guard let fromVc = UIViewController.topMost else {
                return false
            }
            guard let tabBarVc = fromVc.tabBarController else {
                return false
            }
            Observable.just()
                .map{ OrderFormApi.cartcounts }
                .emeRequestApiForRegJson()
                .subscribe(onNext: { (result) in
                    switch result {
                    case .success(let rel):
                        let value = rel.intValue
                        if value == 0 {
                            tabBarVc.tabBar.items?[3].badgeValue = nil
                        }else{
                            tabBarVc.tabBar.items?[3].badgeValue = "\(value)"
                        }
                    case .failure(let error):
                        print(error)
                        
                    }
                })
                .disposed(by: AryaCoreRoute.disposeBag)
            
            
            
            return true
        }
        ///跳转到进货单
        Navigator.map("/orderform") { url, values in
            let orderFormVc:OrderForm_Vc = AryaCore.storyboard.instantiateViewController()
            Navigator.push(orderFormVc)
            return true
        }
    }
    
}
